package use;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class Songs {
    public static void songAdd() throws IOException {
        Scanner sc = new Scanner(System.in);
        String[] song = new String[2];
        System.out.println("请输入歌曲名:");
        song[0] = sc.next();
        System.out.println("请输入歌手:");
        song[1] = sc.next();
        FileWriter fr = new FileWriter("information\\songlibrary.txt",true);
        int i;
        for (i = 0; i < song.length - 1; i++) {
            fr.write(song[i] + ",");
        }
        fr.write(song[i]);
        fr.write("\r\n");
        fr.close();
    }

    public static void starAdd() throws IOException {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入歌手名:");
        String name = sc.next();
        System.out.println("请输入该歌手的性别:");
        String sex = sc.next();
        System.out.println("请输入要录入的歌曲数:");
        int n = sc.nextInt();
        String[] star = new String[n + 2];
        System.out.println("请输入该歌手的歌曲名:");
        star[0] = name;
        star[1] = sex;
        for (int i = 2; i < n + 2; i++) {
            star[i] = sc.next();
        }
        FileWriter fw = new FileWriter("information\\star.txt", true);
        int i;
        for (i = 0; i < star.length-1 ; i++) {
            fw.write(star[i] + ",");
        }
        fw.write(star[n + 1]);
        fw.write("\r\n");
        fw.close();
    }

    public static void songReduce() throws IOException {
        FileReader fr1 = new FileReader("information\\songlibrary.txt");
        BufferedReader bfr1 = new BufferedReader(fr1);
        List<String> list1 = new LinkedList<>();
        String si;
        while ((si = bfr1.readLine()) != null) {
            list1.add(si);
        }
        for (String sg : list1) {
            System.out.println(sg);
        }
        Scanner sca = new Scanner(System.in);
        FileReader fr = new FileReader("information\\songlibrary.txt");
        BufferedReader bfr = new BufferedReader(fr);
        List<String> list = new ArrayList<>();
        String str;
        System.out.println("请输入要删除歌曲的歌名:");
        String name = sca.next();
        while ((str = bfr.readLine()) != null) {
            list.add(str);
            String[] data = str.split(",");
            if (name.equals(data[0])) {
                list.remove(str);
            }
            FileWriter fw = new FileWriter("information\\songlibrary.txt");
            for (String st : list) {

                fw.write(st);
                fw.write("\r\n");
            }
            fw.close();
        }
    }

    public static void starReduce() throws IOException {
        FileReader fr1 = new FileReader("information\\star.txt");
        BufferedReader bfr1 = new BufferedReader(fr1);
        List<String> list1 = new LinkedList<>();
        String si;
        while ((si = bfr1.readLine()) != null) {
            list1.add(si);
        }
        for (String sg : list1) {
            System.out.println(sg);
        }
        Scanner sc = new Scanner(System.in);
        FileReader fr = new FileReader("information\\star.txt");
        BufferedReader bfr = new BufferedReader(fr);
        List<String> list = new ArrayList<>();
        String str;
        System.out.println("请输入要删除明星歌单的明星姓名:");
        String name = sc.next();
        while ((str = bfr.readLine()) != null) {
            list.add(str);
            String[] data = str.split(",");
            if (name.equals(data[0])) {
                list.remove(str);
            }
            FileWriter fw = new FileWriter("information\\star.txt");
            for (String st : list) {
                fw.write(st);
                fw.write("\r\n");
            }
            fw.close();
        }
    }
}